#include "SurfaceList.h"



SurfaceList::SurfaceList()
{
}

SurfaceList::SurfaceList(vector<Surface*> list)
{
	this->list = list;
}


SurfaceList::~SurfaceList()
{
}

Vector3 SurfaceList::GetCenter()
{
	Vector3 s;
	for (auto surf = list.begin(); surf != list.end(); surf++)
	{
		s += (*surf)->GetCenter();
	}
	s /= list.size();
	return s;
}

bool SurfaceList::Hit(lzw::Ray ray, float t0, float t1, HitRecord & rec)
{
	bool res = false;
	for (auto surf = list.begin(); surf != list.end(); surf++)
	{		
		if ((*surf)->Hit(ray, t0, t1, rec))
		{
			res = true;
			t1 = rec.t;
		}
	}
	return res;
}

BoundingBox SurfaceList::GetBoundingBox()
{
	Vector3 min, max;
	min = (*list.begin())->GetBoundingBox().min;
	max = (*list.begin())->GetBoundingBox().max;
	for (auto surf = list.begin() + 1; surf != list.end(); surf++)
	{
		BoundingBox box = (*surf)->GetBoundingBox();
		for (int i = 0; i < 3; i++)
		{
			if (box.min.data[i] < min.data[i])
			{
				min.data[i] = box.min.data[i];
			}
			if (box.max.data[i] > max.data[i])
			{
				max.data[i] = box.max.data[i];
			}
		}
	}
	return BoundingBox(min, max);
}
